package com.yaorange.module.system.controller;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyuncs.exceptions.ClientException;
import com.yaorange.module.config.OssConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.UUID;

@Component
@Slf4j
public class Uploads {

    @Autowired
    OssConfig ossConfig;

    /**
     * 生成一个唯一的 Bucket 名称
     */

    public void upload(String objectName, byte[] content) throws com.aliyuncs.exceptions.ClientException {
        log.info( "创建bucket-{}-{}-{}-{}-{}",ossConfig.getBucketName(), ossConfig.getEndpoint(),  ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret(), ossConfig.getRegin());

        // 从环境变量中获取访问凭证。运行本代码示例之前，请先配置环境变量
        EnvironmentVariableCredentialsProvider credentialsProvider =
                CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(ossConfig.getEndpoint())
                .credentialsProvider(credentialsProvider)
                .region(ossConfig.getRegin())
                .build();

        // 2. 上传文件
        ossClient.putObject(ossConfig.getBucketName(), objectName, new ByteArrayInputStream(content));
        log.info("2. 文件 " + objectName + " 上传成功。");


    }
    public void download(String objectName) throws IOException, ClientException {
        EnvironmentVariableCredentialsProvider credentialsProvider =
                CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(ossConfig.getEndpoint())
                .credentialsProvider(credentialsProvider)
                .region(ossConfig.getRegin())
                .build();
        OSSObject ossObject = ossClient.getObject(ossConfig.getBucketName(), objectName);
        InputStream contentStream = ossObject.getObjectContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(contentStream));
        String line;
        System.out.println("3. 下载的文件内容：");
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }

    public String rename(String fileName) {
        int index = fileName.lastIndexOf(".");
        String substring = fileName.substring(index);
        String s = UUID.randomUUID().toString();
        log.info("文件名-{}",s+fileName);
        return s+"_"+fileName;
    }
}
